package edu.ustb.service;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.Part;

import edu.ustb.Mapper.GoodsMapper;
import edu.ustb.domain.Goods;
import edu.ustb.utils.FileUtil;
import edu.ustb.utils.MybatisUtil;

public class GoodsServiceImpl implements GoodsService{

	@Override
	public void save(String goodsName, String goodsPrice, String goodsNum, String goodsDetail, Part part,
			String savePath) {
		Goods goods = new Goods();
		String realFileName = part.getSubmittedFileName();
		String newFileName = FileUtil.getNewFileName();
		String fileTypeName = FileUtil.getFileTypeName(realFileName);
		String saveGoodsName=newFileName+fileTypeName;
		try {
			part.write(savePath+File.separator+saveGoodsName);
			goods.setGoodsName(goodsName);
			goods.setGoodsNum(Integer.parseInt(goodsNum));
			goods.setGoodsPrice(Double.parseDouble(goodsPrice));
			goods.setGoodsDetail(goodsDetail);
			goods.setGoodsImg(saveGoodsName);
			GoodsMapper goodsMapper = MybatisUtil.getMapper(GoodsMapper.class);
			goodsMapper.save(goods);
			MybatisUtil.commit();
		} catch (IOException e) {
			e.printStackTrace();
			MybatisUtil.rollback();
		}
	}

	@Override
	public List<Goods> showAll(String goodsname, String minprice, String maxprice) {
		GoodsMapper goodsMapper = MybatisUtil.getMapper(GoodsMapper.class);
		List<Goods>goodslist=goodsMapper.showAll(goodsname,minprice,maxprice);
		return goodslist;
	}

	@Override
	public int delete(String goodsId) {
		GoodsMapper mapper = MybatisUtil.getMapper(GoodsMapper.class);
		return mapper.delete(goodsId);
	}

}
